'''
单链表反转：
遍历整个链表，把每个节点的next改为prev。
'''

class ListNode:
    def __init__(self, val=0, next=None):
        self.val = val
        self.next = next

def reverseList(head: ListNode) -> ListNode:
    prev = None
    curr = head
    while curr:
        next_temp = curr.next  # 保存下一个节点
        curr.next = prev       # 反转当前节点的指针
        prev = curr            # 移动prev和curr
        curr = next_temp
    return prev

def printList(head: ListNode):
    curr = head
    while curr:
        print("value: ", curr.val)

        next_temp = curr.next  # 保存下一个节点
        curr = next_temp

a = ListNode(3)
b = ListNode(2, a)
c = ListNode(1, b)

printList(c)

d = reverseList(c)

printList(d)

e = reverseList(d)

printList(e)
